﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
using UML2DOC.Interfaces;
using UML2DOC.ModelProjectEnumerator;
using System.Collections;


namespace UML2DOC.Controller
{
    public class ControllerMain
    {
        private ISolution _curSolution;
        private int _selectedModelProjectIndex;

        public delegate void ClearSolutionFileInfo();
        public event ClearSolutionFileInfo ClearSolutionFileInfoEvent;
        public delegate void UpdateSolutionFileInfo(ISolution solution);
        public event UpdateSolutionFileInfo UpdateSolutionFileInfoEvent;

        public delegate void ClearModelProjects();
        public event ClearModelProjects ClearModelProjectsEvent;
        public delegate void LoadModelProjects(ISolution solution);
        public event LoadModelProjects LoadModelProjectsEvent;

        public delegate void ClearDiagrams();
        public event ClearDiagrams ClearDiagramsInfoEvent;
        public delegate void LoadDiagrams(IList<IDiagram> diagrams);
        public event LoadDiagrams LoadDiagramsInfoEvent;

        public delegate void CreateDocumenation();
        public event CreateDocumenation CreateDocumenationInfoEvent;

        public void CreateUMLDocumenation(IList diagramSelection)
        { 
            /// create docs...
        }

        public ControllerMain()
        {
            _curSolution = null;
        }


        public void GetDiagrams(int SelectedModelProjectIndex)
        {
            ClearDiagramsInfoEvent();

            _selectedModelProjectIndex = SelectedModelProjectIndex;

            if (_curSolution != null)
            {
                // ToDO: remove temp hack that is selecting first model project (0 index). replace with selectedModelProjectIndex

                IList<IDiagram> diagrams = _curSolution.ModelProjects[0].Diagrams;
                LoadDiagramsInfoEvent(diagrams);    
            }
        }

        public void GetSolutionFile()
        {
            FileDialog dlg; // this one must be moved, no UI specific things in the controller
            dlg = new OpenFileDialog();
            dlg.Filter = "VS2010 files (*.sln)|*.sln|All files (*.*)|*.*";
            
            DialogResult result = dlg.ShowDialog();
            string filename = string.Empty;

            if (result == DialogResult.OK)
                filename = dlg.FileName;

            if (filename == null)
            {
                ClearSolutionFileInfoEvent();
                ClearModelProjectsEvent();
                ClearDiagramsInfoEvent();
            }
            else
            {
                _curSolution = Solution.GetSolution(filename);
                UpdateSolutionFileInfoEvent(_curSolution);
                LoadModelProjectsEvent(_curSolution);
            }
        }
    }
}
